package com.samphanie.auiu.auth.security.oauth2;

import cn.hutool.core.util.ObjectUtil;
import com.samphanie.auiu.mbg.model.User;
import lombok.val;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Component
public class JwtTokenEnhancer implements TokenEnhancer {

    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String, Object> info = new HashMap<>();

        if (ObjectUtil.isNotNull(authentication) && authentication.getPrincipal() instanceof User) {
            val user = (User) authentication.getPrincipal();
            // info.put("email", user.getEmail());
            // info.put("mobile", user.getMobile());
            info.put("username", user.getUsername());
        }

        info.put("company", "AUIU-CLOUD");
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(info);
        return accessToken;
    }
}
